<?php
/**********************************************************
  eCode Marketing System
  Version 0.1
  Add Campaign Handler (/admin/handler.campaign_add.php)
**********************************************************/

// Require configuration
define("FROMSCRIPT", true);
require dirname(__FILE__) . "/../config.php";

// Start session if not already started
session_start();

if(!isset($_SESSION['login_t'])) {

	/* If we're not logged in, take us to the login page and
	ensure the rest of the code isn't run */

	header("Location: /admin/login.php");
	exit;
}

// Convert campaign start and end dates to UNIX timestamps, and define today.
$today = time() - 86400;
$campaign_start = strtotime($_POST['start_month'] . "/" . $_POST['start_day'] . "/" . $_POST['start_year']);
$campaign_end = strtotime($_POST['end_month'] . "/" . $_POST['end_day'] . "/" . $_POST['end_year']);

$pagetitle = "Add Campaign (Admin)";
$pageheading = "<a href=\"/admin/index.php\">" . SITE_NAME . " Administration</a> &gt; Add Campaign";
$page_content = "<hr />\n";

// Verify entered campaign
if(!isset($_POST['campaign_name']) || $_POST['campaign_name'] == "") {
	// No campaign name specified
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: No campaign name was specified. Please enter a valid campaign name.</div>\n";
} else if($campaign_start == 0) {
	// Campaign start date not valid
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: Campaign start date is not valid. Please enter a valid campaign start date.</div>\n";
} else if($campaign_end == 0) {
	// Campaign end date not valid
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: Campaign end date is not valid. Please enter a valid campaign end date.</div>\n";
} else if($campaign_start < $today) {
	// Campaign start date cannot be backdated
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: Campaign start date cannot be backdated. Please enter a starting date that is after today's date.</div>\n";
} else if($campaign_end < $today) {
	// Campaign end date cannot be backdated
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: Campaign end date cannot be backdated. Please enter an ending date that is after today's date.</div>\n";
} else if($campaign_start > $campaign_end) {
	// Campaign start and end dates reversed
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: Campaign start and end dates are reversed. Please reenter the start and end dates in the proper order.</div>\n";
} else if($db->get_row("SELECT * FROM `" . DB_TBL_PREFIX . "campaigns` WHERE `campaign_name` = '" . $db->escape($_POST['campaign_name']) . "'")) {
	// Campaign already exists in the database
	$page_content .= "<div id=\"error-box\"><span class=\"error\">ERROR</span>: The campaign name '" . $_POST['campaign_name'] . "' is already in use. Please enter a different campaign name.</div>\n";
} else {
	// Add campaign to the database
	$db->query("INSERT INTO `" . DB_TBL_PREFIX . "campaigns` (campaign_id, campaign_name, start_date, end_date) VALUES (NULL,'". $db->escape($_POST['campaign_name']) ."','" . date("Y-m-d",$campaign_start) . "','" . date("Y-m-d",$campaign_end) . "')");
	$page_content .= "<div id=\"success-box\">The '" . $_POST['campaign_name'] . "' campaign was successfully added.</div>\n";
}

$page_content .= "<p class=\"centered\"><a href=\"/admin/list_campaigns.php\">Back</a> to Campaign List</p>\n";
$page_content .= "<hr />\n";
$page_content .= "<p><a href=\"/admin/index.php\">Back</a> to " . SITE_NAME . " Administration</p>\n";

// Require template
require TEMPLATE_DIR . "/index.php";
?>